package com.shop2cn.iapi.coupon.testcase;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.internal.runners.TestMethod;

import com.shop2cn.iapi.coupon.parameter.getProductDetailCouponListBean;
import com.shop2cn.iapi.coupon.service.getProductDetailCouponListCall;
import com.shop2cn.iapi.couponmanage.parameter.CreateCouponBean;
import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.shop2cn.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnCallService;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.utils.TestDataManager;
/**
*商详页优惠券列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getProductDetailCouponList{
	private static getProductDetailCouponListBean bean;
	private static getProductDetailCouponListCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商详页优惠券列表");
	}

	@Before
	public void caseUp() {
		bean=new getProductDetailCouponListBean();
		call=new getProductDetailCouponListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDetailCouponList_001() {
		Logger.start(true,"商详页优惠券列表-商户-未领取");
		try {
			
	        //创建商家优惠券
			SellerCreateBatchBean sellerCreateBatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall createCouponCall= CouponShop2cnMCallService.SellerCreateBatch(sellerCreateBatchBean);
			
			bean.setMchId(sellerCreateBatchBean.getSellerId());
			bean.setUserId(TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("0", call.getStatusCode1(), "验证返回code");
	        Logger.verifyEquals("操作成功！", call.getMessage(), "验证返回Message");
	        JSONObject jObject=call.getCouponList().getJSONObject(0);
	        
	        Logger.verifyEquals(createCouponCall.getBatchCode(), jObject.getString("batchCode"), "验证batchCode");
	        Logger.verifyEquals(false, jObject.getBoolean("receive"), "验证receive");
	        Logger.verifyEquals(true, jObject.getBoolean("availableReceive"), "验证availableReceive");
	        Logger.verifyEquals(1, jObject.getInt("receiveTimes"), "验证receiveTimes");
	        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDetailCouponList_002() {
		Logger.start(true,"商详页优惠券列表-商户-已领取");
		try {
			
			Integer userId=TestDataManager.getRandomIntNum(5);
			CouponShop2cnCallService couponShop2cnCallService=new CouponShop2cnCallService();
	        //创建商家优惠券
			SellerCreateBatchBean sellerCreateBatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall createCouponCall= CouponShop2cnMCallService.SellerCreateBatch(sellerCreateBatchBean);
			//领取优惠券
			couponShop2cnCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			bean.setMchId(sellerCreateBatchBean.getSellerId());
			bean.setUserId(userId);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("0", call.getStatusCode1(), "验证返回code");
	        Logger.verifyEquals("操作成功！", call.getMessage(), "验证返回Message");
	        JSONObject jObject=call.getCouponList().getJSONObject(0);
	        
	        Logger.verifyEquals(createCouponCall.getBatchCode(), jObject.getString("batchCode"), "验证batchCode");
	        Logger.verifyEquals(true, jObject.getBoolean("receive"), "验证receive");
	        Logger.verifyEquals(false, jObject.getBoolean("availableReceive"), "验证availableReceive");
	        Logger.verifyEquals(1, jObject.getInt("receiveTimes"), "验证receiveTimes");
	        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDetailCouponList_003() {
		Logger.start(true,"商详页优惠券列表-商户-每人可以领取两次，已经领取一次");
		try {
			
			Integer userId=TestDataManager.getRandomIntNum(5);
			CouponShop2cnCallService couponShop2cnCallService=new CouponShop2cnCallService();
	        //创建商家优惠券
			SellerCreateBatchBean sellerCreateBatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellerCreateBatchBean.setReceiveNumPerUser(2);
			SellerCreateBatchCall createCouponCall= CouponShop2cnMCallService.SellerCreateBatch(sellerCreateBatchBean);
			//领取优惠券
			couponShop2cnCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			bean.setMchId(sellerCreateBatchBean.getSellerId());
			bean.setUserId(userId);
			call.setData(bean);
			call.callService();			

			
			Logger.verifyEquals("0", call.getStatusCode1(), "验证返回code");
	        Logger.verifyEquals("操作成功！", call.getMessage(), "验证返回Message");
	        JSONObject jObject=call.getCouponList().getJSONObject(0);
	        
	        Logger.verifyEquals(createCouponCall.getBatchCode(), jObject.getString("batchCode"), "验证batchCode");
	        Logger.verifyEquals(true, jObject.getBoolean("receive"), "验证receive");
	        Logger.verifyEquals(true, jObject.getBoolean("availableReceive"), "验证availableReceive");
	        Logger.verifyEquals(2, jObject.getInt("receiveTimes"), "验证receiveTimes");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDetailCouponList_004() {
		Logger.start(true,"商详页优惠券列表-商户-每人可以领取两次，已经领取两次");
		try {
			
			Integer userId=TestDataManager.getRandomIntNum(5);
			CouponShop2cnCallService couponShop2cnCallService=new CouponShop2cnCallService();
	        //创建商家优惠券
			SellerCreateBatchBean sellerCreateBatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellerCreateBatchBean.setReceiveNumPerUser(2);
			SellerCreateBatchCall createCouponCall= CouponShop2cnMCallService.SellerCreateBatch(sellerCreateBatchBean);
			//领取优惠券
			couponShop2cnCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			couponShop2cnCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			bean.setMchId(sellerCreateBatchBean.getSellerId());
			bean.setUserId(userId);
			call.setData(bean);
			call.callService();			
			
			
			Logger.verifyEquals("0", call.getStatusCode1(), "验证返回code");
	        Logger.verifyEquals("操作成功！", call.getMessage(), "验证返回Message");
	        JSONObject jObject=call.getCouponList().getJSONObject(0);
	        
	        Logger.verifyEquals(createCouponCall.getBatchCode(), jObject.getString("batchCode"), "验证batchCode");
	        Logger.verifyEquals(true, jObject.getBoolean("receive"), "验证receive");
	        Logger.verifyEquals(false, jObject.getBoolean("availableReceive"), "验证availableReceive");
	        Logger.verifyEquals(2, jObject.getInt("receiveTimes"), "验证receiveTimes");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDetailCouponList_005() {
		Logger.start(true,"商详页优惠券列表-商户-未领取-币种与优惠券币种不一样");
		try {
			
	        //创建商家优惠券
			SellerCreateBatchBean sellerCreateBatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall createCouponCall= CouponShop2cnMCallService.SellerCreateBatch(sellerCreateBatchBean);
			
			bean.setMchId(sellerCreateBatchBean.getSellerId());
			bean.setUserId(TestDataManager.getRandomIntNum(5));
			bean.setCurrency("USA");
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("0", call.getStatusCode1(), "验证返回code");
	        Logger.verifyEquals("操作成功！", call.getMessage(), "验证返回Message");
	        Logger.verifyEquals(0, call.getCouponList().length(), "验证返回CouponList的长度");
	        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDetailCouponList_006() {
		Logger.start(true,"商详页优惠券列表-商户-未领取-MchId为空");
		try {
			
			//bean.setMchId(sellerCreateBatchBean.getSellerId());
			bean.setUserId(TestDataManager.getRandomIntNum(5));
			bean.setCurrency("USA");
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("1", call.getStatusCode1(), "验证返回code");
	        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}